import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.utils import formataddr


def sendmail(subject, body):
    mail_host = 'mail.test.com' # 邮件服务器
    mail_user = 'send@test.com' # 登陆账号
    mail_pass = 'test123' # 密码
    postman = 'send@test.com' # 发件人
    toman = 'testuser@test.com' # 收件人
    msg = MIMEText(body, 'html', 'utf-8')
    msg['From'] = formataddr(["域名到期提醒", postman])
    # msg['From'] = postman
    msg['To'] = toman
    msg['Subject'] = Header(subject, 'utf-8')
    mail = smtplib.SMTP(mail_host, 587, timeout=10)
    mail.login(mail_user, mail_pass)
    try:
        mail.sendmail(postman, toman, msg.as_string())
        print('mail send success.')
    except:
        print('mail send fail!')